package com.junmo.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.junmo.common.exception.JunmoBootException;
import com.junmo.common.exception.RemindException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 通用响应体封装
 *
 * @author haoxr
 * @date 2022/1/30
 **/
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    @ApiModelProperty(value = "返回代码")
    private String code = "0" ;

    @ApiModelProperty(value = "返回数据对象")
    private T data;

    @ApiModelProperty(value = "返回处理消息")
    private String msg = "操作成功";

    @ApiModelProperty(value = "数据条数")
    private Long total;

    @ApiModelProperty(value = "时间戳")
    private Long timestamp = System.currentTimeMillis();

    public Result() {
    }

    private Result(IResultCode resultCode) {
        this(resultCode, null, resultCode.getMsg());
    }

    private Result(IResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private Result(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMsg());
    }

    private Result(IResultCode resultCode, T data, String msg) {
        this(msg, resultCode.getCode(), data);
    }

    private Result(String message, String code, T result) {
        this.msg = message;
        this.code = code;
        this.data = result;
        this.success = HttpServletResponse.SC_OK == Integer.parseInt(code);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        ResultCode rce = ResultCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            rce = ResultCode.SYSTEM_EXECUTION_ERROR;
        }
        return result(rce, data);
    }

    public static <T> Result<T> success(T data, Long total) {
        Result<T> result = new Result<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMsg(ResultCode.SUCCESS.getMsg());
        result.setData(data);
        result.setTotal(total);
        return result;
    }

    public static <T> Result<Object> error(JunmoBootException e) {
        return Result.failed(e.getCode(), e.getDefaultMessage());
    }

    public static <T> Result<Object> error(RemindException e) {
        return Result.failed(e.getCode(), e.getDefaultMessage());
    }

    public static <T> Result<T> error(IResultCode resultCode) {
        return new Result<>(resultCode);
    }

    public static <T> Result<T> failed() {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), ResultCode.SYSTEM_EXECUTION_ERROR.getMsg(), null);
    }

    public static <T> Result<T> failed(String msg) {
        return result(ResultCode.SYSTEM_EXECUTION_ERROR.getCode(), msg, null);
    }

    public static <T> Result<T> failed(String msg,String code) {
        return result(code, msg, null);
    }

    public static <T> Result<T> judge(boolean status) {
        if (status) {
            return success();
        } else {
            return failed();
        }
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return result(resultCode.getCode(), resultCode.getMsg(), null);
    }

    public static <T> Result<T> failed(IResultCode resultCode, String msg) {
        return result(resultCode.getCode(), msg, null);
    }

    private static <T> Result<T> result(IResultCode resultCode, T data) {
        return result(resultCode.getCode(), resultCode.getMsg(), data);
    }

    private static <T> Result<T> result(String code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }


    public static boolean isSuccess(Result<?> result) {
        return result != null && ResultCode.SUCCESS.getCode().equals(result.getCode());
    }
}
